package com.zzedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zzedu.common.annotation.DataScope;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.common.utils.bean.BeanUtils;
import com.zzedu.domain.ZzAppDevice;
import com.zzedu.domain.ZzSchool;
import com.zzedu.domain.ZzTeacher;
import com.zzedu.dto.ZzAppDeviceDTO;
import com.zzedu.mapper.ZzAppDeviceMapper;
import com.zzedu.mapper.ZzTeacherMapper;
import com.zzedu.service.IZzAppDeviceService;
import com.zzedu.service.IZzSchoolService;
import com.zzedu.vo.ZzAppDeviceVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 大屏管理Service业务层处理
 *
 * @author Mr.Wang
 * @date 2025-06-02
 */
@Service
public class ZzAppDeviceServiceImpl extends ServiceImpl<ZzAppDeviceMapper, ZzAppDevice> implements IZzAppDeviceService {

    @Autowired
    IZzSchoolService zzSchoolService;

    @Autowired
    ZzTeacherMapper zzTeacherMapper;

    @Override
    public LambdaQueryWrapper<ZzAppDevice> buildQuery(ZzAppDeviceDTO zzAppDevice) {
        LambdaQueryWrapper<ZzAppDevice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzAppDevice.getDeviceSn()), ZzAppDevice::getDeviceSn, zzAppDevice.getDeviceSn());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzAppDevice.getDeviceMac()), ZzAppDevice::getDeviceMac, zzAppDevice.getDeviceMac());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzAppDevice.getLastLoginIp()), ZzAppDevice::getLastLoginIp, zzAppDevice.getLastLoginIp());
        lambdaQueryWrapper.eq(null != zzAppDevice.getState(), ZzAppDevice::getState, zzAppDevice.getState());
        lambdaQueryWrapper.eq(null != zzAppDevice.getSchoolId(), ZzAppDevice::getSchoolId, zzAppDevice.getSchoolId());
        if (null != zzAppDevice.getOnLine()) {
            if (0 == zzAppDevice.getOnLine()) {
                lambdaQueryWrapper.ge(ZzAppDevice::getPreLineTime, LocalDateTime.now().plusMinutes(-30L));
            }
        }
        if (CollectionUtils.isNotEmpty(zzAppDevice.getSchoolIdArray())) {
            lambdaQueryWrapper.in(ZzAppDevice::getSchoolId, zzAppDevice.getSchoolIdArray());
        }
        lambdaQueryWrapper.orderByDesc(ZzAppDevice::getId);
        return lambdaQueryWrapper;
    }

    @DataScope(otherIdColumnName = "school_id")
    public <E extends IPage<ZzAppDevice>> E page(E page, Wrapper<ZzAppDevice> queryWrapper) {
        return super.page(page, queryWrapper);
    }

    @Override
    public List<ZzAppDeviceVO> convertVO(List<ZzAppDevice> zzAppDeviceList) {
        if (CollectionUtils.isNotEmpty(zzAppDeviceList)) {
            return zzAppDeviceList.stream().map(zzAppDevice -> {
                        ZzAppDeviceVO zzAppDeviceVO = new ZzAppDeviceVO();
                        BeanUtils.copyBeanProp(zzAppDeviceVO, zzAppDevice);
                        if (null != zzAppDevice.getSchoolId()) {
                            ZzSchool zzSchool = zzSchoolService.getById(zzAppDevice.getSchoolId());
                            if (null != zzSchool) {
                                zzAppDeviceVO.setSchoolName(zzSchool.getSchoolName());
                            }
                        }
                        if (null != zzAppDevice.getLastLoginUserId()) {
                            ZzTeacher zzTeacher = this.zzTeacherMapper.selectById(zzAppDevice.getLastLoginUserId());
                            if (null != zzTeacher) {
                                zzAppDeviceVO.setLastLoginUserName(zzTeacher.getTel());
                            }
                        }

                        return zzAppDeviceVO;
                    }
            ).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }
}